<?php
class Absence {

   /**
    * A generated check time from absence machine
    *
    * @var string
    * @access protected
   */
   protected $checkTime = null;

   /**
    * Part of on-duty time rules
    * This is a start limit of on-duty checktime.
    * A user is considered and approved to be present (hadir)
    * when its checktime >= $absenceInBegin
    *
    * @var string
    * @access protected
   */
   protected $absenceInBegin = null;

   /**
    * Part of on-duty time rules
    * @var string
    * @access protected
   */
   protected $absenceInEnd = null;

   /**
    * Part of off-duty time rules
    * This is a start limit of off-duty checktime.
    * A user is considered and approved to be leaving
    * when its checktime >= $absenceOutBegin
    *
    * @var string
    * @access protected
   */
   protected $absenceOutBegin = null;

   /**
    * Part of off-duty time rules
    * @var string
    * @access protected
   */
   protected $absenceOutEnd = null;

   /**
    * Part of on-duty time rules
    * A time tolerance for late user. If user's on-duty checktime
    * is equal $lateTimeTolerance, then user is still considered and approved
    * to be present.
    *
    * @var string
    * @access protected
   */
   protected $lateTimeTolerance = null;

   /**
    * Part of off-duty time rules
    * A time tolerance for leaving. If user's off-duty checktime
    * is equal $$leaveTimeTolerance, then user is still considered and approved
    * to be off-duty.
    *
    * @var string
    * @access protected
   */
   protected $leaveTimeTolerance = null;

   /**
    * @param Array $workTime
    * $workTime = array(
      "checkTime" => "00:00:00",
      "absenceInBegin" => "00:00:00",
      "absenceInEnd" => "00:00:00",
      "absenceOutBegin" => "00:00:00",
      "absenceOutEnd" => "00:00:00",
      "lateTimeTolerance" => "00:00:00",
      "leaveTimeTolerance" => "00:00:00",
    )
   */
   public function __construct($workTime = array()) {
      $this->checkTime = $workTime["checkTime"];
      $this->absenceInBegin = $workTime["absenceInBegin"];
      $this->absenceInEnd = $workTime["absenceInEnd"];
      $this->absenceOutBegin = $workTime["absenceOutBegin"];
      $this->absenceOutEnd = $workTime["absenceOutEnd"];
      $this->lateTimeTolerance = $workTime["lateTimeTolerance"];
      $this->leaveTimeTolerance = $workTime["leaveTimeTolerance"];
   }

   /**
    * Get check time attr value
   */
   public function getCheckTime() {
      return $this->checkTime;
   }

   /**
    * Get absenceInBegin attr value
   */
   public function getAbsenceInBegin() {
      return $this->absenceInBegin;
   }

   /**
    * Get absenceInEnd attr value
   */
   public function getAbsenceInEnd() {
      return $this->absenceInEnd;
   }

   /**
    * Get absenceOutBegin attr value
   */
   public function getAbsenceOutEnd() {
      return $this->absenceOutBegin;
   }

   /**
    * Get lateTimeTolerance attr value
   */
   public function getLateTimeTolerance() {
      return $this->lateTimeTolerance;
   }

   /**
    * Get leaveTimeTolerance attr value
   */
   public function getLeaveTimeTolerance() {
      return $this->leaveTimeTolerance;
   }

   /**
    * Checking user's checktime status. Whether a user meets the rule
    * of on-duty time
    *
    * @access public
    * @return boolean - true | false
   */
   public function isAbsenceIn() {
      $ret = false;

      if ((strtotime($this->checkTime) >= strtotime($this->absenceInBegin)) && (strtotime($this->checkTime) <= strtotime($this->absenceInEnd))) {
         $ret = true;
      } else {
         if ((strtotime($this->checkTime) >= strtotime($this->lateTimeTolerance)) && (strtotime($this->checkTime) <= strtotime($this->lateTimeTolerance))) {
            $ret = true;
         }
      }

      return $ret;
   }

   /**
    * Checking user's checktime status. Whether a user meets the rule
    * of off-duty time
    *
    * @access public
   */
   public function isAbsenceOut() {
      $ret = false;

      if ((strtotime($this->checkTime) >= strtotime($this->absenceOutBegin)) && (strtotime($this->checkTime) <= strtotime($this->absenceOutEnd))) {
         $ret = true;
      } else {
         if ((strtotime($this->checkTime) >= strtotime($this->leaveTimeTolerance)) && (strtotime($this->checkTime) <= strtotime($this->leaveTimeTolerance))) {
            $ret = true;
         }
      }

      return $ret;
   }

   /**
    * Test and check leave early condition
    *
    * @access public
    * @return boolean true | false
   */
   public function isLeaveEarly() {
      $ret = false;

      if (strtotime($this->checkTime) < strtotime($this->absenceOutBegin)) {
         $ret = true;
      }

      return $ret;
   }

   /**
    * Test and check come late condition
    *
    * @access public
    * @return boolean true | false
   */
   public function isComeLate() {
      $ret = false;

      if ((strtotime($this->checkTime) >= strtotime($this->absenceInEnd)) && (strtotime($this->checkTime) <= strtotime($this->lateTimeTolerance))) {
         $ret = true;
      }

      return $ret;
   }

}
